<?php namespace Phpcmf\Model\Quan;

// 自动执行方法
class Wenda extends \Phpcmf\Model
{

    
	/*问答奖励积分*/
	public function jianglijifen($tablename, $dirname, $leixing) {
	    $tablezhongbiao = $tablename.'_wendazhongbiao';
		$tablewenda = $tablename.'_form_wenda';
		$table = $tablename.'_form_son';
	    $id = (int)\Phpcmf\Service::L('input')->get('id');
		$uid = (int)\Phpcmf\Service::L('input')->get('uid');
		$leixings = (int)\Phpcmf\Service::L('input')->get('leixing');
		$quanid = (int)\Phpcmf\Service::L('input')->get('quanid');
		$this->index = $this->db->table($table)->where('id', $id)->get()->getRowArray();
		if ($this->index['title']) {
			$datatitle = $this->index['title'];
		} else {
			$datatitle = dr_strcut(dr_clearhtml($this->index['neirong']), 70);
		}
		//dr_debug('$indexuid', $this->index);
	    if (!$this->uid) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '还没有登录',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    } elseif (!$id) {
			$msg = dr_string2array(['status' => 0,
			    'value' => 'id参数不完整',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    }
	
	    if (!$this->db->tableExists($table) ) {
	        return dr_return_data(0, dr_lang('没有问答中标数据库表[%s]', $dirname));
	    }
		
		
		if ($this->index['uid'] != $this->uid) {
			
			$msg = dr_string2array(['status' => 0,
			    'value' => '您不是提问者，无权操作'.$id,
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		} /*else {
			$msg = dr_string2array(['status' => 0,
			    'value' => '操作'.$id,
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}*/
		//判断用户是否提交答案
		$shifoutijiao = $this->db->table($tablewenda)->where('tid', $id)->where('uid', $uid)->get()->getRowArray();
		$title = dr_strcut(dr_clearhtml($shifoutijiao['daan']), 20); //获取帖子内容;
		if (!$shifoutijiao) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '没有这位用户的答案',
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}
	
	    $jindu = $this->db->table($tablezhongbiao)->where('cid', $id)->where('leixing', $leixings)->where('uid', $uid)->get()->getRowArray();
	    if ($jindu) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '已奖励'.SITE_SCORE.$leixings,
				'jifen' => '',]);
			return dr_return_data(1, $msg, $c);

	    } else {
			$val = 1;
			$rmb = 0.1;
			if ($leixings == '1') {
				
				//积分奖励
				if ($this->member['score'] < $val) {
					$msg = dr_string2array(['status' => 0,
					    'value' => '您的'.SITE_SCORE.'是 '.$this->member['score'].'已经不足奖励',
						'jifen' => '',]);
					return dr_return_data(0, $msg);
				}
				//减去发布问题者的积分
				\Phpcmf\Service::M('member')->add_score($this->uid, -$val, $note = '', $url = '', $mark = '', $count = 0);
				
				//给提交答案者增加积分
				\Phpcmf\Service::M('member')->add_score($uid, $val, $note = '', $url = '', $mark = '', $count = 0);
				$jianglifangshi = "积分奖励";
				//发布通知给用户
				$dataim = [
					 'catid'=> 14, //类型
					 'title' => '答案状态', //标题
					 'zhiding' => '', //置顶
					 'content'=> '('.$jianglifangshi.'):'.$title, //通知内容
					 'uid'=> $this->uid, //发布人的uid
					 'faxinren'=> $this->uid, //发布人的uid
					 'shouxinren'=> $uid,//收信人
					 'tupian'=> '', //图片
					 'shipin'=> '', //视频
					 'yuyin'=> '', //语音
					 'fujian'=> '', //附件
					 'yidu'=> 0, //已读
					 'quanziid'=> $shifoutijiao['cid'], //圈子ID
					 'quantieid'=> $shifoutijiao['tid'], //圈帖ID
					 'url' => '',
					];
					\Phpcmf\Service::M('auth')->addtongzhi($dataim);
				//奖励积分提示
				$msg = dr_string2array(['status' => 1,
				    'value' => '操作成功',
					'jifen' => ',奖励 '.$val.' 积分',]);
			} elseif ($leixings == '2'){
				//人民币奖励
				if ($this->member['money'] < $rmb) {
					$msg = dr_string2array(['status' => 0,
					    'value' => '您的余额是 '.$this->member['money'].'已经不足奖励',
						'jifen' => '',]);
					return dr_return_data(0, $msg);
				}
				//减去发布问题者的金额
				\Phpcmf\Service::M('member')->add_money($this->uid, -$rmb);
				\Phpcmf\Service::M('Pay')->add_paylog([
				    'uid' => $this->uid,
				    'username' => "",
				    'touid' => "",
				    'tousername' => '',
				    'mid' => 'system', // 特征字符串，system表示系统
				    'title' => $datatitle,
				    'value' => -$rmb, // 交易金额，正数表示增加金额，负数表示消费金额
				    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
				    'status' => 1, // 1表示交易成功,0表示未成功
				    'result' => $id."问答奖励",
				    'paytime' => SYS_TIME, // 付款时间，默认当前时间
				    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
				]);
				//给提交答案者增加金额
				
				\Phpcmf\Service::M('member')->add_money($uid, $rmb);
				\Phpcmf\Service::M('Pay')->add_paylog([
				    'uid' => $uid,
				    'username' => "",
				    'touid' => "",
				    'tousername' => '',
				    'mid' => 'system', // 特征字符串，system表示系统
				    'title' => $datatitle,
				    'value' => $rmb, // 交易金额，正数表示增加金额，负数表示消费金额
				    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
				    'status' => 1, // 1表示交易成功,0表示未成功
				    'result' => $id."问答奖励",
				    'paytime' => SYS_TIME, // 付款时间，默认当前时间
				    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
				]);
				$jianglifangshi = "金币奖励";
				//发布通知给用户
				$dataim = [
					 'catid'=> 14, //类型
					 'title' => '答案状态', //标题
					 'zhiding' => '', //置顶
					 'content'=> '('.$jianglifangshi.'):'.$title, //通知内容
					 'uid'=> $this->uid, //发布人的uid
					 'faxinren'=> $this->uid, //发布人的uid
					 'shouxinren'=> $uid,//收信人
					 'tupian'=> '', //图片
					 'shipin'=> '', //视频
					 'yuyin'=> '', //语音
					 'fujian'=> '', //附件
					 'yidu'=> 0, //已读
					 'quanziid'=> $shifoutijiao['cid'], //圈子ID
					 'quantieid'=> $shifoutijiao['tid'], //圈帖ID
					 'url' => '',
					];
					\Phpcmf\Service::M('auth')->addtongzhi($dataim);
				//奖励积分提示
				$msg = dr_string2array(['status' => 1,
				    'value' => '操作成功',
					'jifen' => ',奖励 '.$rmb.' 人民币',]);
			} elseif ($leixings == '3'){
				$jianglifangshi = "感谢参与";
				//发布通知给用户
				$dataim = [
					 'catid'=> 14, //类型
					 'title' => '答案状态', //标题
					 'zhiding' => '', //置顶
					 'content'=> '('.$jianglifangshi.'):'.$title, //通知内容
					 'uid'=> $this->uid, //发布人的uid
					 'faxinren'=> $this->uid, //发布人的uid
					 'shouxinren'=> $uid,//收信人
					 'tupian'=> '', //图片
					 'shipin'=> '', //视频
					 'yuyin'=> '', //语音
					 'fujian'=> '', //附件
					 'yidu'=> 0, //已读
					 'quanziid'=> $shifoutijiao['cid'], //圈子ID
					 'quantieid'=> $shifoutijiao['tid'], //圈帖ID
					 'url' => '',
					];
					\Phpcmf\Service::M('auth')->addtongzhi($dataim);
				//感谢参与
				$msg = dr_string2array(['status' => 1,
				    'value' => '操作成功',
					'jifen' => ',感谢参与',]);
				\Phpcmf\Service::M('member')->notice($uid, 1, '《感谢参与》'.$datatitle, $url = '', $mark = '');
			} elseif ($leixings == '4'){
				//中标
				if($this->index['sjlx'] == '1'){
					$rmb = $this->index['wdsj'];
					\Phpcmf\Service::M('member')->add_score($this->uid, intval(-$rmb), $datatitle, $url = '', $mark = '', $count = 0);
					\Phpcmf\Service::M('member')->add_score($uid, intval($rmb), $datatitle, $url = '', $mark = '', $count = 0);
					$msg = dr_string2array(['status' => 1,
					    'value' => '操作成功',
						'jifen' => ',恭喜您中标获得 '.intval($rmb).SITE_SCORE,]);
				} else {
					//给提交答案者增加金额
					$rmb = $this->index['wdsj'];
					\Phpcmf\Service::M('member')->add_money($uid, $rmb);
					\Phpcmf\Service::M('Pay')->add_paylog([
					    'uid' => $uid,
					    'username' => "",
					    'touid' => "",
					    'tousername' => '',
					    'mid' => 'system', // 特征字符串，system表示系统
					    'title' => $datatitle,
					    'value' => $rmb, // 交易金额，正数表示增加金额，负数表示消费金额
					    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
					    'status' => 1, // 1表示交易成功,0表示未成功
					    'result' => $id."问答奖励",
					    'paytime' => SYS_TIME, // 付款时间，默认当前时间
					    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
					]);
					$jianglifangshi = "中标通知";
					//发布通知给用户
					$dataim = [
						 'catid'=> 14, //类型
						 'title' => '答案状态', //标题
						 'zhiding' => '', //置顶
						 'content'=> '('.$jianglifangshi.'):'.$title, //通知内容
						 'uid'=> $this->uid, //发布人的uid
						 'faxinren'=> $this->uid, //发布人的uid
						 'shouxinren'=> $uid,//收信人
						 'tupian'=> '', //图片
						 'shipin'=> '', //视频
						 'yuyin'=> '', //语音
						 'fujian'=> '', //附件
						 'yidu'=> 0, //已读
						 'quanziid'=> $shifoutijiao['cid'], //圈子ID
						 'quantieid'=> $shifoutijiao['tid'], //圈帖ID
						 'url' => '',
						];
						\Phpcmf\Service::M('auth')->addtongzhi($dataim);
					$msg = dr_string2array(['status' => 1,
					    'value' => '操作成功',
						'jifen' => ',恭喜您中标获得 '.$rmb.' 人民币',]);
				}
				
			}
			
			if($rmb == 0){
				$this->db->table($tablezhongbiao)->insert(array(
					'cid' => $id,
					'leixing' => $leixings,
					'shenhe' => 1,
					'jiangli' => 0,
					'jianglirmb' => 0,
					'uid' => $uid,
					'inputtime' => SYS_TIME,
					'endtime' => SYS_TIME
				));
			}else{
				if($this->index['sjlx'] == '1'){
					$this->db->table($tablezhongbiao)->insert(array(
						'cid' => $id,
						'leixing' => $leixings,
						'shenhe' => 1,
						'jiangli' => intval($rmb),
						'jianglirmb' => 0,
						'uid' => $uid,
						'inputtime' => SYS_TIME,
						'endtime' => SYS_TIME
					));
				} else {
					$this->db->table($tablezhongbiao)->insert(array(
						'cid' => $id,
						'leixing' => $leixings,
						'shenhe' => 1,
						'jiangli' => 0,
						'jianglirmb' => $rmb,
						'uid' => $uid,
						'inputtime' => SYS_TIME,
						'endtime' => SYS_TIME
					));
				}
			}
				
			//更新
			//$this->db->table($table)->where('id', $id)->update(['jindu' => 1]);
	    }
		
	
	    // 更新缓存
	    //$c = $this->db->table($tablename.'_form_son_zan')->where('cid', $id)->countAllResults();
	    //$this->db->table($tablename.'_form_son')->where('id', $id)->set('zan', $c)->update();
	    \Phpcmf\Service::M('cache')->update_data_cache();
	
	    // 返回结果
	    return dr_return_data(1, $msg, $c);
	
	}
	/*问答审核*/
	public function daanshenhe($tablename, $dirname, $leixing) {
	    $tablezhongbiao = $tablename.'_wendazhongbiao';
		$tablewenda = $tablename.'_form_wenda';
		$table = $tablename.'_form_son';
	    $id = (int)\Phpcmf\Service::L('input')->get('id');
		$uid = (int)\Phpcmf\Service::L('input')->get('uid');
		$tid = (int)\Phpcmf\Service::L('input')->get('tid');
		$quanid = (int)\Phpcmf\Service::L('input')->get('quanid');
		$this->index = $this->db->table($table)->where('id', $tid)->get()->getRowArray();
		if ($this->index['title']) {
			$datatitle = $this->index['title'];
		} else {
			$datatitle = dr_strcut(dr_clearhtml($this->index['neirong']), 70);
		}
		//dr_debug('$indexuid', $this->index);
	    if (!$this->uid) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '还没有登录',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    } elseif (!$id) {
			$msg = dr_string2array(['status' => 0,
			    'value' => 'id参数不完整',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    }
	    if (!$this->db->tableExists($table) ) {
	        return dr_return_data(0, dr_lang('没有问答中标数据库表[%s]', $dirname));
	    }
		$admin=dr_member_info($this->uid);
		if($admin['is_admin']){
			
		} elseif ($this->index['uid'] != $this->uid) {
			
			$msg = dr_string2array(['status' => 0,
				'value' => '您不是提问者，无权操作',
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		} /*else {
			$msg = dr_string2array(['status' => 0,
			    'value' => '操作'.$id,
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}*/
		//判断用户是否提交答案
		$shifoutijiao = $this->db->table($tablewenda)->where('id', $id)->where('uid', $uid)->get()->getRowArray();
		if (!$shifoutijiao) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '没有这位用户的答案',
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}
		$daanshenhe = $this->db->table($tablewenda)->where('id', $id)->where('uid', $uid)->where('daanshenhe',1)->get()->getRowArray();
		$title = dr_strcut(dr_clearhtml($daanshenhe['daan']), 20); //获取帖子内容;
		if ($daanshenhe) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '已审核',
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		} else {
			$this->db->table($tablewenda)->where('id', $id)->update(['daanshenhe' => 1]);
			//发布通知给用户
			$dataim = [
				 'catid'=> 13, //类型
				 'title' => '答案审核通知', //标题
				 'zhiding' => '', //置顶
				 'content'=> '(已审):'.$title, //通知内容
				 'uid'=> $this->uid, //发布人的uid
				 'faxinren'=> $this->uid, //发布人的uid
				 'shouxinren'=> $uid,//收信人
				 'tupian'=> '', //图片
				 'shipin'=> '', //视频
				 'yuyin'=> '', //语音
				 'fujian'=> '', //附件
				 'yidu'=> 0, //已读
				 'quanziid'=> $daanshenhe['cid'], //圈子ID
				 'quantieid'=> $daanshenhe['tid'], //圈帖ID
				 'url' => '',
				];
				\Phpcmf\Service::M('auth')->addtongzhi($dataim);
			$msg = dr_string2array(['status' => 0,
			    'value' => '已经审核成功',
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}
	    // 更新缓存
	    //$c = $this->db->table($tablename.'_form_son_zan')->where('cid', $id)->countAllResults();
	    //$this->db->table($tablename.'_form_son')->where('id', $id)->set('zan', $c)->update();
	    \Phpcmf\Service::M('cache')->update_data_cache();
	
	    // 返回结果
	    return dr_return_data(1, $msg, $c);
	
	}

}